package com.glorysoft.tonglingxjpda.ui.login.view

import android.app.ActivityManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.ui.setting.view.SettingActivity
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.ui.login.viewmodel.LoginViewModel
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivityLoginBinding
import com.glorysoft.tonglingxjpda.ui.home.view.HomeActivity
import com.glorysoft.tonglingxjpda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

/**
 * FileName: LoginActivity
 * Author: XJH
 * Date: 2021/5/12 18:37
 * Description:
 * History:
 */
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() , EasyPermissions.PermissionCallbacks, View.OnClickListener {

//    private val regions = arrayListOf("氧化","CU","其他","含砷","OLED","TAO")  //铜陵
//    private val regions = arrayListOf("FTSVT")   //天津
//    private val regions = arrayListOf("FTSVG")   //广州
    private val regions = arrayListOf("DL-SUR-XJ")   //大连
    private var regionPop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun initVariableId(): Int {
        return BR.loginViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initUser(this)
//        tvRegion.text = "FTSVG"
        tvRegion.text = "DL-SUR-XJ"   //大连
        viewModel.defUI.startActivity.observe(this){
            startActivity(HomeActivity::class.java)
            finish()
        }
    }

    override fun initData() {
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
        flRegion.setOnClickListener(this)

    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.btnLogin ->viewModel.login(tvRegion.text.toString())
            R.id.ivSetting ->startActivity(SettingActivity::class.java)
            R.id.flRegion ->{
                if (regionPop == null) {
                    regionPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flRegion.width,
                        regions,
                        IvRegion,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvRegion.text = codeS
                            }

                        })

                }

                if (regionPop!!.isShowing) {
                    regionPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(IvRegion)
                    regionPop?.showAsDropDown(flRegion, 0, 0)
                }
            }
        }
    }
}